import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'

import Layout from '../view/layout/index.vue'
import {useUserStore} from '@/pinia/modules/user'
export const LOGIN_URL = "/login";

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/view/login/index.vue'),
  },
  {
    path: '/',
    component: Layout,
    redirect: '/',
    children: [{
      path: '/home',
      name: 'home',
      component: () => import('@/view/home/index.vue'),
    }]
  },{
    path: '/index',
    name: 'index',
    component: () => import('@/view/index/index.vue'),
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

/**
 * @description 路由拦截 beforeEach
 * */
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore();

  if (to.path === LOGIN_URL) {
    if (userStore.token) return next(from.fullPath);
    resetRouter();
    return next();
  }
  if (!userStore.token) return next({ path: LOGIN_URL, replace: true });
  next();
});

/**
 * @description 重置路由
 * */
export const resetRouter = () => {

};

export default router
